package com.zzy.mccjt.gateway.common.filter.privacy;

import com.zzy.mccjt.gateway.common.properties.PrivacyHandleProperties;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author likejie
 * @version 1.0
 * @description: 隐私数据处理-->> 解密
 * @date 2019/8/28 20:12
 */
@Slf4j
@Service("privacyDecryptHandle")
public class PrivacyDecryptHandle/* extends AbstractPrivacyHandle*/ {

/*

    @Override
    public PrivacyHandleProperties.HandleTarget needFilter(HashMap<String, PrivacyHandleProperties.HandleTarget> targets, List<PrivacyHandleProperties.HandleTarget> pers, String uri) {
        if(null != targets && targets.size() > 0){
            //固定匹配
            if (null != targets.get(uri)) {
                PrivacyHandleProperties.HandleTarget handleTarget = targets.get(uri);
                //入参input 解密
                if (null != handleTarget.getInput() && handleTarget.getInput().size() > 0) {
                    return handleTarget;
                }
            }
        }
        if(null != pers && pers.size() > 0) {
            //前缀匹配
            for(PrivacyHandleProperties.HandleTarget handleTarget : pers){
                if(!StringUtils.isEmpty(handleTarget.getUri())
                        && uri.startsWith(handleTarget.getUri())
                        && null != handleTarget.getInput()
                        && handleTarget.getInput().size() > 0){
                    return handleTarget;
                }
            }
        }
        return null;
    }
    @Override
    public HashMap<String, String> paramReady(HashMap<String, String> params, PrivacyHandleProperties.HandleTarget target, String key, Object o, JSONObject jo) {
        //这里key需要是密钥key
        //检查key是否包含密钥key后缀
        if (!key.contains(PrivacyConstant.ENC_FIELD_SUFFIX)) {
            return params;
        }
        //拿到待解密属性名称
        String attrName = key.replace(PrivacyConstant.ENC_FIELD_SUFFIX, "");
        //拿到入参待解密属性配置
        HashMap<String, String> inConfMap = target.getInput();
        //批量准备待解密参数 格式如下-> key：待加密属性  value：对应属性密钥,加密级别模板id  (英文逗号分割)
        if (StringUtils.isEmpty(inConfMap.get(attrName))) {
            return params;
        }
        //默认为 配置的属性名称
        String realName = attrName;
        String confVal = inConfMap.get(attrName);
        //默认为 配置的值
        String modelNo = confVal;
        //包含“#”表示需要映射为另外的属性名
        if(confVal.contains("#")){
            String[] vArr = confVal.split("#");
            //不符合格式要求 不处理
            if(vArr.length != 2){
                return params;
            }
            modelNo = vArr[0];
            realName = vArr[1];
        }
        log.info("-------->解密属性：" + realName);
        //key：待解密属性  value：对应属性密钥,加密级别模板no (英文逗号分割)
        //检查密钥值
        if (!StringUtils.isEmpty(jo.getString(key))) {
            params.put(realName, jo.getString(key) + "," + modelNo);
            log.info("-------->解密属性：" + realName + "密钥为：" + o.toString());
        }
        return params;
    }
    @Override
    public void handle(JSONObject joAdd, HashMap<String, String> params) {
        log.info("-------->发起解密");
        //调用解密服务 批量解密 目前为同子树同阶层属性的批量
        */
/*if (null != params && params.size() > 0) {
            MapReqDTO dto = new MapReqDTO();
            dto.setParams(params);
            try {
                WebApiResponse<Map<String, String>> response = privacyHandleInterface.batchDec(dto);
                if (response.isSuccess()) {
                    Map<String, String> map = response.getData();
                    joAdd.putAll(map);
                    log.info("-------->解密成功" + map.toString());
                } else {
                    log.info("-------->解密失败" + response.getMessage());
                    throw new BusinessException(SystemError.PRIVACY_DEC_ERROR.getCode(), "解密异常,请刷新重试");
                }
            } catch (FeignException e) {
                log.error(e.getMessage(),e);
                throw new BusinessException(SystemError.PRIVACY_DEC_ERROR.getCode(), "解密异常,请刷新重试" + e.getMessage());
            }
        }*//*

    }
*/

}
